package dp.gr03.payroll.application;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InputParser
{

    /*
     * This Pattern will match on either quoted text or text between space, including
     * whitespace, and accounting for beginning and end of line.
     */
    private final Pattern pattern = Pattern.compile("\"([^\"]*)\"|(?<= |^)([^ ]*)(?: |$)");
    private ArrayList<String> allMatches = null;
    private Matcher matcher = null;

    public InputParser()
    {
        allMatches = new ArrayList<String>();
        matcher = null;
    }

    public String[] parse(String line)
    {
        matcher = pattern.matcher(line);
        allMatches.clear();
        String match;
        while (matcher.find())
        {
            match = matcher.group(1);
            if (match != null)
            {
                allMatches.add(match);
            } else
            {
                allMatches.add(matcher.group(2));
            }
        }
        List<String> results = new ArrayList<String>();
        for (int i = 0; i < allMatches.size(); i++)
        {
            if (!allMatches.get(i).equals(""))
            {
                results.add(allMatches.get(i).trim());
            }
        }
        if (results.size() > 0)
        {
            return results.toArray(new String[results.size()]);
        } else
        {
            return new String[0];
        }
    }
}